#include "editpunishments.h"
#include "ui_editpunishments.h"

EditPunishments::EditPunishments(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::EditPunishments)
{
    ui->setupUi(this);

    loadView();
    setupComboBox();
    setActionsEnabled(false);

    connect(ui->comboBox,        SIGNAL(currentIndexChanged(int)), this, SLOT(updateTableView()));
    connect(ui->plusToolButton,  SIGNAL(clicked()),                this, SLOT(increaseSentry()));
    connect(ui->minusToolButton, SIGNAL(clicked()),                this, SLOT(decreaseSentry()));
}


void EditPunishments::setupComboBox()
{
    ui->comboBox->clear();

    QSqlQuery *query = new QSqlQuery();
    query->exec("SELECT class FROM students GROUP BY class");
    while(query->next()){
        ui->comboBox->addItem(query->value(0).toString());
    }

    QString currentItemText = ui->comboBox->currentText();

    tableModel->setFilter(QString("class = '%1'").arg(currentItemText));
    tableModel->select();
}


void EditPunishments::updateTableView()
{
    QString currentItemText = ui->comboBox->currentText();

    tableModel->setFilter(QString("class = '%1'").arg(currentItemText));
    tableModel->select();

    setActionsEnabled(false);
}


void EditPunishments::setActionsEnabled(bool _bool)
{
    QSqlRecord record = tableModel->record(curIndex.row());
    int punSentC = record.value("sentryP").toInt();

    ui->plusToolButton->setEnabled(_bool);

    if(punSentC <= 0)
        ui->minusToolButton->setEnabled(false);
    else if(!_bool)
            ui->minusToolButton->setEnabled(false);
        else
            ui->minusToolButton->setEnabled(true);
}


void EditPunishments::loadView()
{
    tableModel = new QSqlTableModel(this);
    tableModel->setTable("students");
    tableModel->setSort(Students_Class, Qt::AscendingOrder);
    tableModel->setHeaderData(Students_Number,  Qt::Horizontal, tr("Number"));
    tableModel->setHeaderData(Students_Class,   Qt::Horizontal, tr("Class"));
    tableModel->setHeaderData(Students_Name,    Qt::Horizontal, tr("Name"));
    tableModel->setHeaderData(Students_Sentry1, Qt::Horizontal, tr("Sentry1"));
    tableModel->setHeaderData(Students_Sentry2, Qt::Horizontal, tr("Sentry2"));
    tableModel->setHeaderData(Students_Sentry3, Qt::Horizontal, tr("Sentry3"));
    tableModel->setHeaderData(Students_Sentry4, Qt::Horizontal, tr("Sentry4"));
    tableModel->setHeaderData(Students_SentryT, Qt::Horizontal, tr("SentryT"));
    tableModel->setHeaderData(Students_SentryP, Qt::Horizontal, tr("SentryP"));
    tableModel->select();

    ui->tableView->setModel(tableModel);
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableView->setColumnHidden(Students_ID,      true);
    ui->tableView->setColumnHidden(Students_Number,  false);
    ui->tableView->setColumnHidden(Students_Class,   false);
    ui->tableView->setColumnHidden(Students_Name,    false);
    ui->tableView->setColumnHidden(Students_Sentry1, true);
    ui->tableView->setColumnHidden(Students_Sentry2, true);
    ui->tableView->setColumnHidden(Students_Sentry3, true);
    ui->tableView->setColumnHidden(Students_Sentry4, true);
    ui->tableView->setColumnHidden(Students_SentryT, true);
    ui->tableView->setColumnHidden(Students_SentryP, false);

    ui->tableView->resizeColumnsToContents();
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);

    QHeaderView *header = ui->tableView->horizontalHeader();
    header->setStretchLastSection(true);
}


EditPunishments::~EditPunishments()
{
    delete ui;
}


void EditPunishments::on_tableView_pressed(const QModelIndex &index)
{
    curIndex = index;
    setActionsEnabled(true);
}


void EditPunishments::decreaseSentry()
{
    curIndex = ui->tableView->currentIndex();

    QMessageBox::StandardButton ans;
    ans = QMessageBox::information(this,
                                   tr("Decrease Sentry"),
                                   tr("Are you sure you wish to decrease one sentry?"),
                                   QMessageBox::Yes | QMessageBox::No);
    if (ans == QMessageBox::Yes)
    {
        QSqlRecord record = tableModel->record(curIndex.row());
        int newC = record.value("sentryP").toInt() + (-1);

        query.prepare("update students set sentryP = :sentry where id = :id");
        query.bindValue(":sentry",      newC);
        query.bindValue(":id",          record.value("id").toString());
        query.exec();

        setActionsEnabled(false);
        tableModel->select();
        return;
    }
    return;
}


void EditPunishments::increaseSentry()
{
    curIndex = ui->tableView->currentIndex();

    QMessageBox::StandardButton ans;
    ans = QMessageBox::information(this,
                                   tr("Increase Sentry"),
                                   tr("Are you sure you wish to increase one sentry?"),
                                   QMessageBox::Yes | QMessageBox::No);
    if (ans == QMessageBox::Yes)
    {
        QSqlRecord record = tableModel->record(curIndex.row());
        int newC = record.value("sentryP").toInt() + (1);

        query.prepare("update students set sentryP = :sentry where id = :id");
        query.bindValue(":sentry",      newC);
        query.bindValue(":id",          record.value("id").toString());
        query.exec();

        setActionsEnabled(false);
        tableModel->select();
        return;
    }
    return;
}
